from common.exception.base_exception import BaseBizException
from common.exception.biz_error_type import BizErrorType


class Result:
    SUCCESS_CODE = 0
    SUCCESS_MSG = "success"

    def __init__(self, code=None, msg=None, data=None):
        self.code = code
        self.msg = msg
        self.data = data

    @classmethod
    def success(cls, data=None):
        return cls(cls.SUCCESS_CODE, cls.SUCCESS_MSG, data)

    @classmethod
    def fail(cls, code, msg=None, data=None):
        return cls(code, msg, data)

    @classmethod
    def fail_with_base_exception(cls, base_exception: BaseBizException, data=None):
        return cls(base_exception.get_error_type().get_code(), base_exception.get_error_type().msg, data)

    @classmethod
    def fail_with_custom_code(cls, code, msg, data=None):
        return cls(code, msg, data)

    @classmethod
    def fail_with_error_type(cls, error_type: BizErrorType, data=None):
        return cls(error_type.get_code(), error_type.get_msg(), data)

    def is_success(self):
        return self.code == self.SUCCESS_CODE

    def is_fail(self):
        return not self.is_success()

    def get_code(self):
        return self.code

    def get_msg(self):
        return self.msg

    def get_data(self):
        return self.data

    def set_code(self, code):
        self.code = code

    def set_msg(self, msg):
        self.msg = msg

    def set_data(self, data):
        self.data = data

    def to_json(self):
        return {'code': self.code, 'msg': self.msg, 'data': self.data}
